//
//  AppDelegate.m
//  WhatToEat
//
//  Created by Mac on 15/3/18.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "AppDelegate.h"
#import "MobClick.h"

//社会化分享
#import "UMSocialWechatHandler.h"
#import "UMSocial.h"
#import "UMSocialSinaHandler.h"
#import "UMSocialQQHandler.h"
#import "EAFoodAndWaterController.h"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
  //  [self mobClickAction];
   
    
    self.tabooFoods=[@[] mutableCopy];

    
    [self setNavigationPreferences];
    [self initDDMenu];
    
    
    if (self.userId) {
        
        [self updateUserInfo];
        self.window.rootViewController=[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"MainViewControllerNV"];
    }
    
    [[UITextField appearance] setTintColor:[UIColor blackColor]];
    
//    self.window.rootViewController=[[EAFoodAndWaterController alloc] init];
    
    self.window.backgroundColor=[UIColor whiteColor];
//    [self setUMSocialShareAction];
    NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
    [NSURLCache setSharedURLCache:urlCache];

    return YES;
}


-(void)setUMSocialShareAction
{
[UMSocialData setAppKey:MobClickAppKey];

    //打开调试log的开关
    [UMSocialData openLog:YES];
    


    
    //设置微信AppId，设置分享url，默认使用友盟的网址
    [UMSocialWechatHandler setWXAppId:weiXinAppID appSecret:weixinAppSecret url:shareUrl];
    
    //打开新浪微博的SSO开关
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    
    
    
    

    
    //    //设置分享到QQ空间的应用Id，和分享url 链接
    [UMSocialQQHandler setQQWithAppId:tenxinOpenAppID appKey:tenxinOpenAppIDKey url:shareUrl];
    [UMSocialQQHandler setSupportWebView:YES];
    
    
}





#pragma mark----回调函数
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [UMSocialSnsService handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url];
}




-(void)mobClickAction
{
    [MobClick startWithAppkey:MobClickAppKey reportPolicy:BATCH   channelId:@""];
    
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [MobClick setAppVersion:version];


    

}


-(void)updateUserInfo
{

    [WebAPI webAPISignAddMd5tWithNote:@"user/getUserInfo" parm:@{@"userID":self.userId} completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
       
        
        
        
        [EASaveUserMessage saveUserMessage:responseObject andView:nil];
        
        
        
    }];
    


}



#pragma mark----设置导航栏偏好

-(void)setNavigationPreferences
{
    
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg10"] forBarMetrics:UIBarMetricsDefault];
    
    [[UINavigationBar appearance] setTintColor:kTingColor];
    
    
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                          
                                                          
                                                          
                                                          [UIColor whiteColor],
                                                          NSForegroundColorAttributeName,
                                                          [UIColor whiteColor],
                                                          NSForegroundColorAttributeName,
                                                          [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
                                                          NSForegroundColorAttributeName,
                                                          
                                                   
                                                          
                                                          nil]];
    
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    

     }





#pragma mark----侧栏
-(void)initDDMenu
{
    
    self.ddVc=[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"DDMenuController"];
    
    self.rootVc=[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"MainViewControllerNV"];
    



    self.myVc=[self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

    
   self.ddVc.rootViewController=self.rootVc;
//    self.ddVc.leftViewController=self.myVc;

    


}


#pragma mark-------自动保存用户字段---用户id

-(void)setUserId:(NSString *)userId
{

    [NSUserDefaultstandard setObject:userId forKey:@"userId"];
    [NSUserDefaultstandard synchronize];

}

-(NSString *)userId
{

  return [NSUserDefaultstandard objectForKey:@"userId"];
    

}


-(void)setUseName:(NSString *)useName
{
    [NSUserDefaultstandard setObject:useName forKey:@"useName"];
    [NSUserDefaultstandard synchronize];

}

-(NSString *)useName
{

    return [NSUserDefaultstandard objectForKey:@"useName"];
    

}

-(void)setHeadImage:(NSString *)headImage
{
    [NSUserDefaultstandard setObject:headImage forKey:@"headImage"];
    [NSUserDefaultstandard synchronize];

}

-(NSString *)headImage
{
    return [NSUserDefaultstandard objectForKey:@"headImage"];

}


-(void)setMyTabooFoods:(NSMutableArray *)myTabooFoods
{

    [NSUserDefaultstandard setObject:myTabooFoods forKey:@"myTabooFoods"];
    [NSUserDefaultstandard synchronize];


}

-(NSMutableArray *)myTabooFoods
{

    return [NSUserDefaultstandard objectForKey:@"myTabooFoods"];
}


-(void)setUserDictionary:(NSDictionary *)userDictionary
{

    [NSUserDefaultstandard setObject:userDictionary forKey:@"userDictionary"];
    [NSUserDefaultstandard synchronize];



}

-(NSDictionary *)userDictionary
{

    return [NSUserDefaultstandard objectForKey:@"userDictionary"];

}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


#pragma mark-----上传菜谱到服务器
-(void)sendMealWithMeals:(NSMutableArray *)meal andMealType:(NSString *)mealType andAty_level:(NSString *)aty_level
{
       NSString * meals=[Commtools  toJsonStringFrom:meal];
//    
//    
//   NSString *ss = [meal componentsJoinedByString:@" "];

    
    
    
    NSDictionary * parms=@{
                           @"meal":meals,
                           
                           @"mealType":mealType,
                           
                           @"userID":self.userId,
                           @"aty_level":aty_level
                           };
    
    [WebAPI webAPISignAddMd5tWithNote:@"user/getMealFromAPP" parm:parms completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
       
        
        
        if ([kStr(responseObject[@"code"]) isEqualToString:@"1"]) {
            
            
            [SVProgressHUD showImage:nil status:@"加入餐桌成功"];
            

        
            UITabBarController *TVC=(UITabBarController *)ApplicationDelegate.window.rootViewController;
            
            
            UINavigationController * lvc=TVC.viewControllers[0];
            
            
            [lvc popToRootViewControllerAnimated:YES];
            

            
            
            

        }
        
        UITabBarController *TVC=(UITabBarController *)ApplicationDelegate.window.rootViewController;
        
        
        UINavigationController * lvc=TVC.viewControllers[0];
        
        
        [lvc popToRootViewControllerAnimated:YES];
        
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:updateInMyTable object:mealType];
        

        

    }];
    
}
@end
